<?php

namespace app\admin\controller;

use app\common\controller\BaseCommon;
use app\common\service\AdminPublicService;
use think\Cache;


class User extends  BaseCommon
{
    protected $adminPublicService;

    public function __construct(AdminPublicService $adminPublicService)
    {
        parent::__construct();
        $this->adminPublicService = $adminPublicService;
    }

    /**
     * @api {post} api/admin/public/login 登陆【后台】
     * @apiVersion 1.0.0
     * @apiName login
     * @apiGroup Public
     *
     * @apiParam {Int} username 用户名（手机号）
     * @apiParam {Int} password 密码
     *
     * @apiSuccess (响应  200) {number} code 请求成功状态码
     * @apiSuccess (响应  200) {json} data 返回数据
     * @apiSuccess (响应  200) {string} [msg='""'] 返回消息说明
     * @apiSuccess (响应  200) {number} time 请求接口时间
     *
     * @apiUse CODE_4xx
     *
     * @apiSampleRequest api/admin/public/login
     *
     */
    public function login()
    {
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        $validate = $this->validate(
            ['username' => $username, 'password' => $password],
            ['username' => 'require', 'password' => 'require'],
            ['username' => '请填写用户名', 'password' => '请填写密码']
        );
        if (true !== $validate) {
            $this->apiResult('', 404, $validate);
        }
        $res = $this->adminPublicService->adminLogin($username, $password);
        if ($res['status'] == 1) {
            $this->apiResult($res['data'], 200, '登陆成功');
        } else {
            $this->apiResult('', 404, $res['msg']);
        }
    }

    /**
     * @api {post} api/admin/public/logout 注销【后台】
     * @apiVersion 1.0.0
     * @apiName logout
     * @apiGroup Public
     *
     *
     * @apiSuccess (响应  200) {number} code 请求成功状态码
     * @apiSuccess (响应  200) {json} data 返回数据
     * @apiSuccess (响应  200) {string} [msg='""'] 返回消息说明
     * @apiSuccess (响应  200) {number} time 请求接口时间
     *
     * @apiUse CODE_4xx
     *
     * @apiSampleRequest api/admin/public/logout
     *
     */
    public function logout()
    {
        Cache::rm('store_admin');
        $this->apiResult('', 200, '退出成功');

    }
}
